package com.efounder.frame.fragment;

import android.net.http.SslError;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.core.xml.StubObject;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.frame.utils.OSPPlugin;
import com.efounder.mobilemanager.R;

import java.io.Serializable;
import java.util.List;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;
import static com.efounder.frame.utils.Constants.KEY_SETTING_APPID;

/**
 * Created by lch on 2017/6/14.
 */

public class EFAPPWebFragment extends BaseFragment {
    private int position;
    private List<StubObject> twoLevelStubList;
    private WebView webView;

    public static EFAPPWebFragment newInstance(int position, List<StubObject> twoLevelStubList) {
        EFAPPWebFragment appAccountFragment = new EFAPPWebFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putSerializable("twoLevelStubList", (Serializable) twoLevelStubList);
        appAccountFragment.setArguments(bundle);
        return appAccountFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        position = getArguments().getInt("position");
        twoLevelStubList = (List<StubObject>) getArguments().getSerializable("stubList");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ef_fragment_webview, container, false);
        //加载页面
        webView = (WebView) view.findViewById(R.id.XWalkView);
        //允许JavaScript执行
        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true);

        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        webView.clearCache(true);
        //webView.clearHistory();

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                Toast.makeText(EFAPPWebFragment.this.getContext(), message,
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });


        webView.setWebViewClient(new WebViewClient() {
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, SslError error) {
                //handler.cancel(); 默认的处理方式，WebView变成空白页
                handler.proceed(); // 接受证书
            }
        });
        StubObject stubObject = twoLevelStubList.get(position);
        String url = stubObject.getString("url", "");
        if (url.startsWith("http")) {
            webView.loadUrl(url);
        } else {
            String baseUrl = "file:///mnt/sdcard/" + EnvironmentVariable.getProperty(KEY_SETTING_APPID) + "/" + EnvironmentVariable.getProperty(CHAT_USER_ID) + "/Apps/" + url;
            webView.loadUrl(baseUrl);

        }
        //找到Html文件，也可以用网络上的文件
        //webView.loadUrl("file:///android_asset/www/index.html");
        //webView.loadUrl("https://10.69.127.38:88/ESTMobile/SLGX/index.html");
        // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法
        String username = EnvironmentVariable.getUserName();
        String userpass = EnvironmentVariable.getPassword();
        webView.addJavascriptInterface(new OSPPlugin(username, userpass, EFAPPWebFragment.this.getContext()), "OSPPlugin");

        return view;
    }
}
